<?php 
/**
 * This file is part of DomFg.
 * 
 * DomFg is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomFg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomFg.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */

/**
* @author Oliver Leisalu, Inditel Meedia
* @package DomFg
*/
class DomFg {

	public static function render( $structure, $parent = NULL ) {
		$item = self::renderer( $structure, $parent );
		if( $item ) 
			return $item->render();
		return '';
	}

	public static function renderer( $structure, $parent = NULL, $defaultType = NULL ) {
		if( is_array($structure) ) {
			
			// setting defaults
			if( isset($structure['type']) === false ) {
				if( $defaultType === NULL ) 
					$structure['type'] = is_object($parent) ? $parent->getDefaultChild() : 'panel';
				else
					$structure['type'] = $defaultType;
			}

			
			// creating item
			$className = 'DomFg'.ucfirst($structure['type']);
			$item = new $className;



			// add property values.
			$ignore = array('type'=>true,'items'=>true,'buttons'=>true,'headerButtons' => true, 'fields'=>true);
			foreach( $structure as $key => &$value ) {
				if( $ignore[$key] !== true ) {
					if( !method_exists($item, $key) )
						throw new RuntimeException('No such property <strong>'.$key.'</strong> for class <strong>'.get_class($item).'</strong>!');
					$item->$key( $value );
				}
			}
			
			
			
			// add items
			if( is_array($structure['items']) === true ) {
				self::renderItems( $item, $structure['items'] );
			}
			
			// add panel buttons
			if( is_array($structure['buttons']) === true ) {
				if( $item instanceof DomFgPanel )
					self::renderItems( $item, $structure['buttons'], 'button', 'addButton' );
				else
					throw new RuntimeException('No such property <strong>buttons</strong> for class <strong>'.get_class($item).'</strong>!');
			}
			
			// add panel header buttons
			if( isset($structure['headerButtons']) === true ) {
				if( $item instanceof DomFgPanel ) {
					if( $structure['headerButtons'] === 'buttons' ) {
						self::renderItems( $item, $structure['buttons'], 'button', 'addHeaderButton' );
					} else if( is_array($structure['headerButtons']) === true ) {
						self::renderItems( $item, $structure['headerButtons'], 'button', 'addHeaderButton' );
					}
				} else
					throw new RuntimeException('No such property <strong>headerButtons</strong> for class <strong>'.get_class($item).'</strong>!');
			}
			
			// add fields ( for grid )
			if( is_array($structure['fields']) === true ) {
				if( $item instanceof DomFgGrid )
					self::renderItems( $item, $structure['fields'], $item->getDefaultChild(), 'addField' );
				else
					throw new RuntimeException('No such property <strong>fields</strong> for class <strong>'.get_class($item).'</strong>!');
			}
			
		
		} else 
			$item = $structure;
		
		return $item;
	}
	
	public static function renderItems( $parent, array &$items, $defaultType = NULL, $addMethod = 'addItem' ) {
		foreach( $items as $key => &$value ) {
			if( $value['type'] === NULL && $value[0] !== NULL ) {
				self::renderItems( $parent, $value );
			} else {
				$subItem = self::renderer( $value, $parent, $defaultType );
				if( $subItem ) {
					
					// Exception: adds Title cell for Input cells with title.
					if( ($subItem instanceof DomFgInput or $subItem instanceof DomFgValue)&& isset($value['title']) && !$subItem instanceof DomFgButton ) {
						$title = self::renderer( array('value' => $value['title']), $parent, 'title' );
						$parent->$addMethod( $title );
					}
					// ---------------------------------------------
				
				
					// Exception: changes sub panels defaults
					if( $subItem instanceof DomFgPanel && $parent instanceof DomFgPanel) {
						if( !isset($value['frame']) ) $subItem->frame(false);
						if( !isset($value['border']) ) $subItem->border(false);
					}		
					// ---------------------------------------------
					
					$parent->$addMethod( $subItem );
				}
			}
		}
	}
	
	/**
	* @return String
	*/
	public static function getTemplateUrl() {
		return DOM_URL.'DomFg/Templates/Vista/';
	}
	/**
	* @return String
	*/
	public static function getTemplatePath() {
		return DOM_PATH.'DomFg/Templates/Vista/';
	}
	
	private static $headRendered = false;
	public static function renderHead() {
		if( self::$headRendered === false ) {
			self::$headRendered = true;
			DomFgHistory::init();
			$r = new DomSmarty();
			$r->template_dir = self::getTemplatePath();
			$r->assign('templateUrl', self::getTemplateUrl() );
			return $r->fetch('head.html');
		}
	}
	
	private static $headerRendered = false;
	public static function renderHeader() {
		if( self::$headerRendered === false ) {
			self::$headerRendered = true;
			$r = new DomSmarty();
			$r->template_dir = self::getTemplatePath();
			return $r->fetch('header.html');
		}
	}
	
	private static $footerRendered = false;
	public static function renderFooter() {
		if( self::$footerRendered === false ) {
			self::$footerRendered = true;
			$r = new DomSmarty();
			$r->template_dir = self::getTemplatePath();
			$r->assign('tooltips', DomFgToolTip::getTooltipsHtml());
			return $r->fetch('footer.html');
		}
	}


}
?>